package com.ifcar.app.view;

import com.ifcar.app.util.ThemeSettingsHelper;
import com.ifcar.app.activity.R;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.TextView;

public class SearchListView extends FrameLayout {

	private ListView mSearchHistoryView;
	private ListView mSearchResultView;
	private TextView mEmptyOrErrorText;
	private ThemeSettingsHelper themeHelper;
	
	public SearchListView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
	}

	public SearchListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	public SearchListView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	@Override
	protected void onFinishInflate() {
		// TODO Auto-generated method stub
		super.onFinishInflate();
		Context context = getContext();
		themeHelper = ThemeSettingsHelper.getThemeSettingsHelper(context);
		
		mSearchHistoryView = new ListView(context);
		mSearchResultView = new ListView(context);
		mEmptyOrErrorText = new TextView(context);
		
		FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
				FrameLayout.LayoutParams.FILL_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT);
		
		addView(mSearchHistoryView, params);
		addView(mSearchResultView, params);
		
		FrameLayout.LayoutParams params1 = new FrameLayout.LayoutParams(
				FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
		
		params1.gravity = Gravity.CENTER_HORIZONTAL;
		params1.setMargins(10, 10, 10, 10);
		
		addView(mEmptyOrErrorText, params1);

		initStatus(mSearchHistoryView);
		initStatus(mSearchResultView);

		mEmptyOrErrorText.setTextSize(16);
		mEmptyOrErrorText.setVisibility(View.GONE);
		themeHelper.setTextViewColor(context, mEmptyOrErrorText, R.color.list_title_color);
		
	}
	
	/**
	 * 初始化listview状态
	 * @param listview
	 */
	private void initStatus(ListView listview) {
		// TODO Auto-generated method stub
		listview.setVisibility(View.GONE);
		listview.setFadingEdgeLength(0);
		listview.setCacheColorHint(0);
		listview.setSelector(getResources().getDrawable(R.drawable.listview_selector));
		listview.setDividerHeight(0);
//		themeHelper.setListViewDivider(context, listview, R.drawable.list_divider_line);
	}
	
	/**
	 * 搜索建议或者搜索历史
	 * @param adapter
	 */
	public void setHistoryOrSuggestListViewAdapter(BaseAdapter adapter){
		mEmptyOrErrorText.setVisibility(View.GONE);
		mSearchResultView.setVisibility(View.GONE);
		mSearchHistoryView.setVisibility(View.VISIBLE);
		mSearchHistoryView.setAdapter(adapter);
	}
	
	/**
	 * 设置搜索结果listview
	 * @param adapter
	 */
	public void setSearchResultListViewAdapter(BaseAdapter adapter){
		mEmptyOrErrorText.setVisibility(View.GONE);
		mSearchHistoryView.setVisibility(View.GONE);
		mSearchResultView.setVisibility(View.VISIBLE);
		mSearchResultView.setAdapter(adapter);
	}

	/**
	 * 搜索结果为空
	 * @param keyword
	 */
	public void setNoResult4Search(String keyword) {
		// TODO Auto-generated method stub
		mSearchHistoryView.setVisibility(View.GONE);
		mSearchResultView.setVisibility(View.GONE);
		mEmptyOrErrorText.setVisibility(View.VISIBLE);
		if(keyword.length() > 10){
			keyword = keyword.substring(0, 10);
		}
		mEmptyOrErrorText.setText(getResources().getString(R.string.search_no_data, keyword));
	}

	/**
	 * 网络异常
	 */
	public void setNetworkInvalid() {
		// TODO Auto-generated method stub
		mSearchHistoryView.setVisibility(View.GONE);
		mSearchResultView.setVisibility(View.GONE);
		mEmptyOrErrorText.setVisibility(View.VISIBLE);
		mEmptyOrErrorText.setText(R.string.msg_net_error);
	}

	/**
	 * 搜索结果是否为空
	 * @return
	 */
	public boolean isSearchAdapterNULL() {
		// TODO Auto-generated method stub
		return null == mSearchResultView.getAdapter() || mSearchResultView.getAdapter().isEmpty();
	}

	/**
	 * 显示搜索结果list
	 */
	public void showSearchResultView() {
		// TODO Auto-generated method stub
		mSearchHistoryView.setVisibility(View.GONE);
		mSearchResultView.setVisibility(View.VISIBLE);
		mEmptyOrErrorText.setVisibility(View.GONE);
	}

	/**
	 * 获取搜索结果listview
	 */
	public ListView getSearchResultListView() {
		// TODO Auto-generated method stub
		return mSearchResultView;
	}
	
	/**
	 * 获取搜索历史listview
	 */
	public ListView getSearchHistoryListView() {
		// TODO Auto-generated method stub
		return mSearchHistoryView;
	}
}
